package cm.com.carlamanda.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cm.com.carlamanda.domain.UserInfo;

public class LoginInterceptor extends HandlerInterceptorAdapter{
	private static final String[] IGNORE_URI = {"/#login","/plugins/","/dist/","/angular/","/userLogin","/getUserSession","/station/","/userRegister","/userDelete","/userUpdate","/userSearch"};
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean flag = false;
        String url = request.getRequestURL().toString();
        System.out.println(">>>: " + url);
        
        for (String s : IGNORE_URI) {
            if (url.contains(s)) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            UserInfo user = (UserInfo) request.getSession().getAttribute("userGroupInfo");
            if (user != null) 
            	flag = true;
        }
        return flag;
	}
	
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
			throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
}
